<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script src="/layui/layui.js"></script>
    <script src="/js/template-web.js"></script>
</head>

<body>
    <div class="layui-container">
        <table class="layui-table">
            <thead>
                <tr>
                    <th>th01</th>
                    <th>th02</th>
                    <th>th03</th>
                </tr>
            </thead>
            <tbody>

            </tbody>
        </table>
    </div>

    <!-- 定义模板 -->
    <script type="text/html" id="mydemo">
        {{ each arr value index}}
            <tr>
                <td>{{ value.id }}</td>
                <td>{{ value.name }}</td>
                <td>{{ value.age }}</td>
            </tr>
        {{ /each }}

    </script>
    <script>
        function sendAjax() {
            let xhr = new XMLHttpRequest();
            xhr.open("get", "/users");
            xhr.onload = function () {
                let arr = JSON.parse(xhr.responseText);//将json格式的字符串转换为对象格式
                let str=template("mydemo",{arr});
                let dom = document.querySelector("tbody");
                dom.innerHTML = str;
            };
            xhr.send();
        }
        sendAjax();
    </script>
</body>

</html>